package pers.xx.sbm.dy;

import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pers.xx.sbm.dao.DyAnswerJuniorMapper;
import pers.xx.sbm.dao.DyQuestionMapper;
import pers.xx.sbm.dao.DyStudentScoreMapper;
import pers.xx.sbm.entity.DyQuestion;
import pers.xx.sbm.entity.DyStudentScore;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author ：xiexing
 * @description：分数转换 初中
 * @date ：2022/1/18 14:05
 */
@SpringBootTest
public class ScoreJuniorTransfer {
    @Autowired
    DyQuestionMapper dyQuestionMapper;


    @Autowired
    DyAnswerJuniorMapper dyAnswerJuniorMapper;

    @Autowired
    DyStudentScoreMapper dyStudentScoreMapper;

//    @Test
    void getQeustion() throws Exception {
        List<DyQuestion> dyQuestionList = dyQuestionMapper.getQuestionList("初中", "'第一部分','第二部分','第三部分','第四部分','第五部分'");
        List<HashMap<String, String>> dyAnswerJuniorList = dyAnswerJuniorMapper.getAnswerJuniorList();
        List<DyStudentScore> list = scoreTransfer(dyQuestionList, dyAnswerJuniorList);
        dyStudentScoreMapper.insertBatch(list);
    }

    /**
     * @description 分数转化
     * @author XieXing
     * @date 2022/1/19 9:28
     */
    public List<DyStudentScore> scoreTransfer(List<DyQuestion> dyQuestionList, List<HashMap<String, String>> dyAnswerJuniorList) throws Exception {
        List<DyStudentScore> dyStudentScoreList = new ArrayList<>();
        for (HashMap<String, String> obj : dyAnswerJuniorList) {
            String studentName = obj.get("name");//学生姓名
            String testNumber = obj.get("test_number");//学号
            String schoolName = obj.get("school");//学校
            String countyName = obj.get("county_name");//区县名称
            String xd = "初中";
            for (int i = 1; i <= 84; i++) {
                DyStudentScore dyStudentScore = new DyStudentScore();
                if (StringUtils.isBlank(obj.get("t"+i))){
                    System.out.println(testNumber+":"+i);
                }
                String answer = obj.get("t" + i).trim();

                DyQuestion dyQuestion = dyQuestionList.get(i - 1);
                String correct = ("" + dyQuestion.getStandardAnswer()).trim();
                dyStudentScore.setAnswer(answer);
                dyStudentScore.setAreaName(countyName);
                dyStudentScore.setTestNumber(testNumber);
                dyStudentScore.setStudentName(studentName);
                dyStudentScore.setQuestionNumber(i);
                dyStudentScore.setPart(dyQuestion.getPart());
                dyStudentScore.setIndex1(dyQuestion.getIndex1());
                dyStudentScore.setIndex2(dyQuestion.getIndex2());
                dyStudentScore.setIndex3(dyQuestion.getIndex3());
                dyStudentScore.setSchoolName(schoolName);
                dyStudentScore.setXd(xd);
                dyStudentScore.setYear("2021");
                if (i < 8) {
                    if (answer.equals("#")) {
                        dyStudentScore.setScore(0);
                    } else {
                        if (answer.equals(correct)) {
                            dyStudentScore.setScore(5);
                        } else {
                            dyStudentScore.setScore(0);
                        }
                    }
                } else {
                    if (answer.equals("#")) {
//                        scoreVo.setScore("#");
                        dyStudentScore.setScore(1);//没选的按1分处理
                    } else {
                        String scoreType = dyQuestion.getScoreType().trim();
                        if ("正向计分".equals(scoreType)) { //正向计分
                            if (answer.equals("A")) {
                                dyStudentScore.setScore(1);
                            } else if (answer.equals("B")) {
                                dyStudentScore.setScore(2);
                            } else if (answer.equals("C")) {
                                dyStudentScore.setScore(3);
                            } else if (answer.equals("D")) {
                                dyStudentScore.setScore(4);
                            } else if (answer.equals("E")) {
                                dyStudentScore.setScore(5);
                            } else {
                                dyStudentScore.setScore(1);
                            }
                        } else if ("反向计分".equals(scoreType)) { //反向计分
                            if (answer.equals("A")) {
                                dyStudentScore.setScore(5);
                            } else if (answer.equals("B")) {
                                dyStudentScore.setScore(4);
                            } else if (answer.equals("C")) {
                                dyStudentScore.setScore(3);
                            } else if (answer.equals("D")) {
                                dyStudentScore.setScore(2);
                            } else if (answer.equals("E")) {
                                dyStudentScore.setScore(1);
                            } else {
                                dyStudentScore.setScore(1);
                            }
                        }
                    }
                }
                dyStudentScoreList.add(dyStudentScore);
                if (dyStudentScoreList.size() % 60001 == 0) {
                    dyStudentScoreMapper.insertBatch(deepCopy(dyStudentScoreList));
                    dyStudentScoreList.clear();
                }
            }//循环题
        }//循环人
        return dyStudentScoreList;
    }

    /**
     * @description 深度拷贝
     * @author XieXing
     * @date 2022/1/19 15:36
     */
    public static List deepCopy(List<DyStudentScore> src) throws IOException,
            ClassNotFoundException {
        ByteArrayOutputStream byteout = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteout);
        out.writeObject(src);
        ByteArrayInputStream bytein = new ByteArrayInputStream(byteout
                .toByteArray());
        ObjectInputStream in = new ObjectInputStream(bytein);
        List<DyStudentScore> dest = (List<DyStudentScore>) in.readObject();
        return dest;
    }

}
